package com.example.ktapplication.base

import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.ktapplication.R
import com.example.ktapplication.databinding.FragmentTitleRvBinding
import com.example.ktapplication.enum.StateLayoutMode
import com.example.ktapplication.model.SystemViewModel
import com.example.ktapplication.utils.LogHelper
import com.example.ktapplication.utils.event.Event
import com.example.ktapplication.utils.event.EventBusUtil

abstract class BaseTitleRvFragment : BaseFragment<FragmentTitleRvBinding>() {

    val listAdapter by lazy {
        initAdapter()
    }

    abstract fun initAdapter(): RecyclerView.Adapter<RecyclerView.ViewHolder>

    abstract fun loadData()

    abstract fun loadMoreData()

    open fun getTitleBarStr(): String {
        return ""
    }

    override fun getViewBind(): FragmentTitleRvBinding {
        return FragmentTitleRvBinding.inflate(layoutInflater)
    }

    override fun onViewBing() {
        initTitleBar()
        initStateLayout()
        initRvList()
        loadData()
    }

    private fun initTitleBar() {
        val showTitleBar = getTitleBarStr().isNotEmpty()
        binding.titleBar.isVisible = showTitleBar
        if (showTitleBar) {
            binding.titleBar.tvTitle?.text = getTitleBarStr()
            binding.titleBar.imgFind?.setOnClickListener {
                EventBusUtil.eventBus.post(Event.MainClickFind())
            }
            binding.titleBar.imgMenu?.setOnClickListener {
                EventBusUtil.eventBus.post(Event.MainClickMenu())
            }
        }
    }

    private fun initStateLayout() {
        binding.statusLayout.apply {
            val loadLayout = layoutInflater.inflate(R.layout.status_layout_loading, null)
            loadLayout?.let {
                setLoadingLayout(it)
            }
            val emptyLayout = layoutInflater.inflate(R.layout.status_layout_empty, null)
            emptyLayout?.let {
                it.setOnClickListener {
                    stateLayoutMode(StateLayoutMode.LOADING)
                    loadData()
                }
                setEmptyLayout(it)
            }
            val errorLayout = layoutInflater.inflate(R.layout.status_layout_error, null)
            errorLayout?.let {
                it.setOnClickListener {
                    stateLayoutMode(StateLayoutMode.LOADING)
                    loadData()
                }
                setErrorLayout(it)
            }
            val netLayout = layoutInflater.inflate(R.layout.status_layout_net, null)
            netLayout?.let {
                it.setOnClickListener {
                    stateLayoutMode(StateLayoutMode.LOADING)
                    loadData()
                }
                setNetLayout(it)
            }
        }
    }

    open fun initRvList() {
        binding.rvResult.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = listAdapter
        }
        binding.smartRefreshLayout.apply {
            setOnRefreshListener {
                loadData()
            }
            setOnLoadMoreListener {
                loadMoreData()
            }
        }
    }

    open fun loadDataFinish() {
        binding.smartRefreshLayout.finishRefresh()
        binding.smartRefreshLayout.finishLoadMore()
    }

    fun stateLayoutMode(mode: StateLayoutMode) {
        binding.statusLayout.setMode(mode)
    }


    fun SystemViewModel.addViewModelState(action: (StateLayoutMode) -> Unit = {}) {
        reqState.observe(this@BaseTitleRvFragment) {
            LogHelper.e("---ext --addViewModelState-- ${it.name}")
            binding.statusLayout.setMode(it)
            if (it != StateLayoutMode.LOADING)
                action.invoke(it)
        }
    }
}
